#include "projectlistwidget.h"
#include <QMimeData>
ProjectListWidget::ProjectListWidget(QWidget* parent):
QListWidget(parent)
{
    setAcceptDrops(true);
}
void ProjectListWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
        pozit=event->pos();
    QListWidget::mousePressEvent(event);

}

void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->button()&Qt::LeftButton)
    {
        int distance=(event->pos()-pozit).manhattanLength();
    if(distance >=QApplication::startDragDistance())
    performDrag();
    }
    QListWidget::mouseMoveEvent(event);
}


void ProjectListWidget::performDrag()
{
QListWidgetItem *item=currentItem();
if (item)
{
    QMimeData *mimeData=new QMimeData;
    mimeData->setText(item->text());
    QDrag *drag=new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap(":/images/person.png"));
    if (drag->start(Qt::MoveAction)== Qt::MoveAction)
            delete item;
}
}

void ProjectListWidget::dragEnterEvent(QDragEnterEvent*event)
{
    ProjectListWidget*sourse=qobject_cast<ProjectListWidget *>(event->source());
    if (sourse &&sourse!=this)
{
        event->setDropAction(Qt::MoveAction);
    event->accept();
    }
}

void ProjectListWidget::dragMoveEvent(QDragMoveEvent*event)
{
    ProjectListWidget *sourse=qobject_cast<ProjectListWidget*>(event->source());
    if (sourse && sourse !=this)
{
    event->setDropAction(Qt::MoveAction);
event->accept();
    }
 }


    void ProjectListWidget::dropEvent(QDropEvent*event)
    {
        ProjectListWidget*sourse=qobject_cast<ProjectListWidget *>(event->source());
        if (sourse&&sourse!=this)
        {
            addItem(event->mimeData()->text());
            event->setDropAction(Qt::MoveAction);
            event->accept();
        }
    }

